package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.CheckedMeResponse
import com.gexiaobao.pigeon.app.model.bean.FindPigeonResponse
import com.gexiaobao.pigeon.app.model.bean.LookPigeonResponse
import com.gexiaobao.pigeon.app.model.bean.RingContrastResponse
import com.gexiaobao.pigeon.app.model.bean.SearchRaceByNameResponse
import com.gexiaobao.pigeon.app.model.bean.SearchNameResponse
import com.gexiaobao.pigeon.app.model.bean.SearchRaceByNameDetailResponse
import com.gexiaobao.pigeon.app.model.bean.SearchRaceResponse
import com.gexiaobao.pigeon.app.model.bean.VideoInfoResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

/**
 * @author:hxw
 * @date:  2023-02-03
 * @desc:
 */

class MineToolsViwModel : BaseViewModel() {

    private var pageSize: Int = 20
    private var pageNo: Int = 1

    var pigeonMaster = StringObservableField()
    var ringId = StringObservableField()
    var etRingId = StringObservableField()

    //电子环号
    var etRingNumber = StringObservableField()

    var mResultLook = MutableLiveData<ResultState<LookPigeonResponse>>()

    /**  足环对照  */
    var ringContrastList: MutableLiveData<ListDataUiState<RingContrastResponse.DataList>> = MutableLiveData()

    /**  获取视频教程列表  */
    var videoList: MutableLiveData<ListDataUiState<VideoInfoResponse.ListInfo>> = MutableLiveData()

    /**  谁查过我  */
    var checkedMeResult: MutableLiveData<ListDataUiState<CheckedMeResponse.InfoList>> = MutableLiveData()

    fun searchRing(ringId: String, name: String) {
        request({ apiService.searchRing(ringId, name) }, mResultLook, true, appContext.getString(R.string.loading))
    }

    fun ringContrast(isRefresh: Boolean, ringSn: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.ringContrast(ringSn, pageNo, 30) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            ringContrastList.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<RingContrastResponse.DataList>()
            )
            ringContrastList.value = listDataUiState
        })
    }

    /** getVideo */
    fun getVideo(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getVideo(pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            videoList.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<VideoInfoResponse.ListInfo>()
            )
            videoList.value = listDataUiState
        })
    }

    /**
     * 谁查过我
     */
    fun getSearchPigeon(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getSearchPigeon(pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            checkedMeResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<CheckedMeResponse.InfoList>()
            )
            checkedMeResult.value = listDataUiState
        })
    }

    /** 赛鸽模糊查询 */
    var findPigeonResult: MutableLiveData<ListDataUiState<FindPigeonResponse.ListBean>> = MutableLiveData()
    fun searchPigeon(isRefresh: Boolean, ringId: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.searchPigeon(ringId, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                total = it.total,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            findPigeonResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<FindPigeonResponse.ListBean>()
            )
            findPigeonResult.value = listDataUiState
            RxToast.showToast(it.msg)
        })
    }

    /** 赛赛鸽比赛查询 */
    var searchRaceResult: MutableLiveData<ListDataUiState<SearchRaceResponse.ListBean>> = MutableLiveData()
    fun searchRace(isRefresh: Boolean, ringId: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.searchRace(ringId, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                total = it.total,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            searchRaceResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<SearchRaceResponse.ListBean>()
            )
            searchRaceResult.value = listDataUiState
        })
    }

    /** 名称模糊查询 searchName */
    var searchNameResult: MutableLiveData<ListDataUiState<SearchNameResponse.ListBean>> = MutableLiveData()
    fun searchName(isRefresh: Boolean, name: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.searchName(name, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                total = it.total,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            searchNameResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<SearchNameResponse.ListBean>()
            )
            searchNameResult.value = listDataUiState
            RxToast.showToast(it.msg)
        })
    }

    /** 名称模糊查询 searchName */
    var searchRaceByNameResult: MutableLiveData<ListDataUiState<SearchRaceByNameResponse.ListBean>> = MutableLiveData()
    fun searchRaceByName(isRefresh: Boolean, name: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.searchRaceByName(name, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                total = it.total,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            searchRaceByNameResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<SearchRaceByNameResponse.ListBean>()
            )
            searchRaceByNameResult.value = listDataUiState
        })
    }

    /** 根据鸽主赛事名称查询赛事详情 */
    var searchRaceByNameDetailResult: MutableLiveData<SearchRaceByNameDetailResponse> = MutableLiveData()
    fun searchRaceDetail(isRefresh: Boolean, pgName: String, raceName: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.searchRaceDetail(pgName, raceName, pageNo, pageSize) }, {
            pageNo++
            searchRaceByNameDetailResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

}